Skip to content

fix(redirect): merge query string with the one already in the url - #158

Open
bardrop wants to merge 1 commit into
adonisjs:9.xfrom
bardrop-oss:fix/redirect-qs-merge
Open

bardrop wants to merge 1 commit into
adonisjs:9.xfrom
bardrop-oss:fix/redirect-qs-merge

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Fixes #157

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Redirect#sendResponse always built ${url}?${qs}. So when the target url already had a query string or a fragment, the result was broken:

call                                                   before                        after
withQs({ success: 1 }).toPath('/users?page=2')         /users?page=2?success=1       /users?page=2&success=1
withQs({ success: 1 }).toPath('/users?page=2#list')    /users?page=2#list?success=1  /users?page=2&success=1#list
withQs({ success: 1 }).toPath('/users')                /users?success=1              /users?success=1

With a second ? the browser reads page as 2?success=1. After # the query string is part of the fragment and never reaches the server. The same happens with response.redirect(url, true) and forwardQueryString: true.

Now the query string goes before the #, and is joined with & when the url already has a ? (no extra & if the url ends with ? or &). Urls without a query string or fragment are built the same as before.

Tests: added two tests in tests/redirect.spec.ts. Both fail on 9.x and pass with the fix. npm run lint, npm run typecheck and npm run quick:test (675 tests) pass.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly. (no docs change needed)

sendResponse always built "${url}?${qs}", so withQs() or forwarded
query strings gave "/users?page=2?success=1" when the target already had
a query string, and "/users#list?success=1" when it had a fragment.

Join with "&" when the url already has a "?", and keep the fragment at
the end.

Fixes adonisjs#157
@fleet-panda fleet-panda Bot added the status: incoming The status of the hub task label Nov 26, 2026
@thetutlage thetutlage added status: ready The status of the hub task and removed status: incoming The status of the hub task labels Nov 26, 2026
@fleet-panda fleet-panda Bot added status: building The status of the hub task and removed status: ready The status of the hub task labels Nov 26, 2026
@fleet-panda fleet-panda Bot added status: review The status of the hub task and removed status: building The status of the hub task labels Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: review The status of the hub task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

redirect() adds a second ? when the target url already has a query string

2 participants